package com.ww.juc.jd.async.basic;

import java.util.Map;
import java.util.concurrent.TimeUnit;

import com.jd.platform.async.callback.ICallback;
import com.jd.platform.async.callback.IWorker;
import com.jd.platform.async.worker.WorkResult;
import com.jd.platform.async.wrapper.WorkerWrapper;

import cn.hutool.core.date.SystemClock;

public class ParWorker2 implements IWorker<String, String>, ICallback<String, String> {

	@Override
	public void begin() {
		System.out.println(Thread.currentThread().getName() + "- start --" + System.currentTimeMillis());
	}

	@Override
	public void result(boolean success, String param, WorkResult<String> workResult) {
		if (success) {
			System.out.println("回调成功" + SystemClock.now() + "---" + workResult.getResult()
					+ "-threadName:" + Thread.currentThread().getName());
		} else {
			System.out.println("回调失败" + SystemClock.now() + "---" + workResult.getResult()
					+ "-threadName:" + Thread.currentThread().getName());
		}
	}

	@SuppressWarnings("rawtypes")
	@Override
	public String action(String object, Map<String, WorkerWrapper> allWrappers) {
		try {
			TimeUnit.SECONDS.sleep(1L);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		return "result = " + SystemClock.now() + "---param = " + object + " from 2";
	}

	@Override
	public String defaultValue() {
		return "worker2--default";
	}
}
